package com.app.debeziumkafka;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

/**
 * 
 * @author 北京八维博大科技 薛建新
 * @date 2022年12月23日 15:18:06
 * @Copyright 北京八维博大科技
 */
public class InstallMain {
	private static final String PATH = "http://localhost:8083/";

	public static void main(String[] args) {
		//delete();
		install();
	}

	private static void delete() {
		HttpRequest request = HttpRequest.newBuilder().uri(URI.create(PATH + "connectors/conn-8023"))
				.header("Accept", "*/*").method("DELETE", HttpRequest.BodyPublishers.noBody()).build();
		HttpResponse<String> response;
		try {
			response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
			System.out.println(response.body());
			System.out.println("delete success!!!");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	private static void install() {
		String content = """
				    {
					  "name": "conn-8023",
					  "config": {
					    "connector.class"       : "io.debezium.connector.mysql.MySqlConnector",
					    "database.hostname"     : "localhost",
					    "database.port"         : "8023",
					    "database.user"         : "root",
					    "database.password"     : "123456",
					    "database.server.id"    : "8023",
					    "database.server.name"  : "local8023",
					    "include.schema.changes": "true",
					    "database.include.list" : "test,debezium",
					    "topic.prefix"          : "bj",
					    "schema.history.internal.kafka.topic": "dbhistory.local8023",
					    "schema.history.internal.kafka.bootstrap.servers": "localhost:9092",
					    "decimal.handling.mode" : "double",
					    "signal.data.collection": "debezium.debezium_signal"
					  }
					}
				""";
		HttpRequest request = HttpRequest.newBuilder().uri(URI.create(PATH + "connectors")).header("Accept", "*/*")
				.header("Content-Type", "application/json").method("POST", HttpRequest.BodyPublishers.ofString(content))
				.build();
		HttpResponse<String> response;
		try {
			response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
			System.out.println(response.body());
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}
